<include file="header"/>
<link rel="stylesheet" href="/assets/public/mobiscroll/css/mobiscroll.custom-3.0.0-beta2.min.css">
<div class="wraper space">
	<a href="/base/store/address/cart_id/{BEESCRM:$cart_id}/store_id/{BEESCRM:$storeId}/type/1003/picked/{BEESCRM:$picked}" class="list-info status tb-mode">
		<p class="tb-cell"><span class="iconfont icon-location"></span></p>
		<div class="tb-cell">
			<if condition="$data['address']['real_name']">
				{BEESCRM:$data.address.real_name}&nbsp;&nbsp;&nbsp;&nbsp;
			</if>
			<if condition="$data['address']['mobile']">
				{BEESCRM:$data.address.mobile}&nbsp;&nbsp;&nbsp;&nbsp;
			</if>
			<if condition="$picked eq 0 and $data['address']['address_id'] gt 0">
				<if condition="$data['address']['address_detail']">
					{BEESCRM:$data.address.province_name} {BEESCRM:$data.address.city_name} {BEESCRM:$data.address.district_name} {BEESCRM:$data.address.address_detail}
				<else/>
					缺少地址信息，请完善	
				</if>
			</if>
			<if condition="$data['address']['address_id'] lt 1 and $picked eq 0">
				请添加地址
			<elseif condition="$data['address']['address_id'] lt 1 and $picked eq 1"/>
				请添加联系方式
			</if>
		</div>
		<div class="tb-cell"><span class="iconfont icon-you pull-right"></span></div>
	</a>
    <div class="order-list">
        <a href="{BEESCRM::U('base/store/index',array('store_id'=>$storeId))}" class="shop">
        	<span class="iconfont icon-dianpu"></span>
        	{BEESCRM:$data.store_name} <span class="iconfont icon-you"></span>
        </a>
		<volist name="data['cart']" id="vo">
	        <a href="#" class="list-info order text-black">
				<div class="tb-cell product-img">
					<div><img src="{BEESCRM:$vo.goods.goods_thumb}"></div>
				</div>
	            <div class="product-info">
	                <p>{BEESCRM:$vo.goods.goods_name}</p>
	                <p class="prize"><span>￥{BEESCRM:$vo.price}</span> <span>X{BEESCRM:$vo.goods_num}</span></p>
					<p class="text-12">
						<if condition="$vo['goods']['picked'] eq 1">
							<span class="product-label">自提</span>
						<else/>
							<span class="product-label">快递</span>
						</if>
						<if condition="$browser eq 2">
							<if condition="$vo['goods']['pay_alipay_id'] neq $alipayId">
								<span class="product-label">厂家直营</span>
							</if>
						<else/>
							<if condition="$vo['goods']['pay_wechat_id'] neq $wechatId">
								<span class="product-label">厂家直营</span>
							</if>
						</if>
					</p>
	                <if condition="$vo['goods_spec'] neq ''">
	                <p>规格：{BEESCRM:$vo.goods_spec}</p>
	                </if>
	            </div>
	        </a>
			<if condition="$picked eq 1">
				<div class="list">提取地点：{BEESCRM:$vo.since_address}</div>
				<div class="list">商家规则：{BEESCRM:$vo.goods.picked_rule}</div>
			</if>
        </volist>
    </div>

	<div class="form-list">
		<select id="pay_select">
			<if condition ="$browser eq 2">
				<option value="2">支付宝支付</option>
			<elseif condition ="$browser eq 1" />
				<option value="1">微信支付</option>
			</if>
			<if condition = "$accountId eq 28 or $accountId eq 12">
				<option value="3">校园卡支付</option>
			</if>
		</select>
		<span class="iconfont icon-you"></span>
	</div>

	<div class="order-info">
		<textarea placeholder="给卖家留言" class="leave-message" id="remark"></textarea>

		<div class="list">
			<p class="text-right">
				共 <span class="strong-red">{BEESCRM:$data.cart|count}</span> 件商品 &nbsp;&nbsp;&nbsp;
			</p>
		</div>
	</div>

    <div class="btn-box bottom clearfix">
        <p class="pull-left"><span class="account">合计：<span>￥{BEESCRM:$data.total_price}</span></span></p>
		<form id="form_campuscard" method="post" action="{BEESCRM:$interface_ip}/{BEESCRM:$interface_name}">
			<input type="hidden" name="tranamt" value="" /><!--交易金额1-->
			<input type="hidden" name="account" value="" /><!--学生账号-->
			<input type="hidden" name="sno" value="" /><!--学工号-->
			<input type="hidden" name="toaccount" value="" /><!--商户账号1-->
			<input type="hidden" name="thirdsystem" value="" /><!--系统注册ID1-->
			<input type="hidden" name="thirdorderid" value="" /><!--第三方订单号1-->
			<input type="hidden" name="ordertype" value="" /><!--接入类型1-->
			<input type="hidden" name="sign" value="" /><!--sign签名-->
			<input type="hidden" name="orderdesc" value="" /><!--商品名称-->
			<input type="hidden" name="praram1" value="" /><!--第三方系统备注字段-->
			<input type="hidden" name="thirdurl" value="" /><!--第三方系统备注字段-->
		</form>
        <p class="pull-right"><a href="javascript:void(0)" class="btn pay" id="submit">确认下单</a></p>
    </div>
</div>
<input type="hidden" id="status" value="{BEESCRM:$status}" /><!--校园卡绑定状态-->
<input type="hidden" id="address" value="{BEESCRM:$data['address']['address_id']}" />
<include file="commonJs"/>
<script src="/assets/public/mobiscroll/mobiscroll.custom-3.0.0-beta2.min.js"></script>
<script>
	var currYear = (new Date()).getFullYear();
	var opt={};
	opt.date = {preset : 'date'};
	opt.datetime = {preset : 'datetime'};
	opt.time = {preset : 'time'};
	opt.default = {
		theme: 'ios', //皮肤样式
		display: 'bottom', //显示方式
		mode: 'scroller', //日期选择模式
		lang:'zh',
		startYear:currYear - 10, //开始年份
		endYear:currYear + 10 //结束年份
	};

	var optDateTime = $.extend(opt['datetime'], opt['default']);
	var optTime = $.extend(opt['time'], opt['default']);
	$("#date").mobiscroll(optDateTime).datetime(optDateTime);
</script>
<script type="text/javascript">
	$(function() {

        //确认付款
		$(document).on('click', '#submit', function () {
            var address_id = $('#address').val();
			var remark = $('#remark').val();
			var cart_id = "{BEESCRM:$cart_id}";
//			var address_id = "{BEESCRM:$data['address']['address_id']}";
			var openId = "{BEESCRM:$openId}";
			var pay_type = $("#pay_select").val();
			var status = $("#status").val();
			if(pay_type == 3){
				if(status!=1){
					$.alert('error', '校园卡未绑定');
					setTimeout("location.href='index.php?g=base&m=store&a=bound&store_id="+{BEESCRM:$storeId}+"&cart_id="+cart_id+"&user_id="+address_id+"'",2000);
					return;
				}
			}

			$.ajax({
				url: "{BEESCRM::U('base/store/createOrder',array('store_id'=>$storeId))}",
				type: 'POST',
				dataType: 'json',
				data: {
					cart_id: cart_id,
					address_id: address_id,
					remark: remark,
					openId: openId,
					pay_type: pay_type
				},
				success: function (data) {
					if (data.status == 'AS') {
						location.href = data.url;
					} else if (data.status == 'WS') {
						//微信支付
						if (typeof WeixinJSBridge == "undefined") {
							if (document.addEventListener) {
								document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
							} else if (document.attachEvent) {
								document.attachEvent('WeixinJSBridgeReady', jsApiCall);
								document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
							}
						} else {
							WeixinJSBridge.invoke(
									'getBrandWCPayRequest',
									$.parseJSON(data.url),
									function (res) {
										WeixinJSBridge.log(res.err_msg);
										if (res.err_msg == "get_brand_wcpay_request:ok") {
											paySuccess(data.order_id);
										} else if (res.err_msg == "get_brand_wcpay_request:cancel") {
											//	$.alert('error', '你肯定不是真的想抛弃我们～');
											url = "/index.php?g=base&m=store&a=pendingpay&order_id=" + data.order_id + "&store_id=" + "{BEESCRM:$storeId}";
											location.href = url;
											return false;
										} else {
											$.alert('error', '哎呀，网络不给力，小微又调皮了，咋们刷新再来一次');
											setTimeout("location.reload()", 2000);
											return false;
										}
									}
							);
						}
					} else if (data.status == 'CampusCard') {
						$("input[name=tranamt]").val(data.data['tranamt']);
						$("input[name=account]").val(data.data['account']);
						$("input[name=sno]").val(data.data['sno']);
						$("input[name=toaccount]").val(data.data['toaccount']);
						$("input[name=thirdsystem]").val(data.data['thirdsystem']);
						$("input[name=thirdorderid]").val(data.data['thirdorderid']);
						$("input[name=ordertype]").val(data.data['ordertype']);
						$("input[name=sign]").val(data.data['sign']);
						$("input[name=orderdesc]").val(data.data['orderdesc']);
						$("input[name=praram1]").val(data.data['praram1']);
						$("input[name=thirdurl]").val(data.data['thirdurl']);
						$("#form_campuscard").attr("action",data.data['interface_url']);
						$("#form_campuscard").submit();
					} else if (data.status == 'S') {
						paySuccess(data.order_id);
					} else {
						swal("", data.message, "error");
					}
				}
			})

		});

		function paySuccess(order_id) {
			var json = {};
			json.url = "{BEESCRM::U('base/store/paysuccess',array('origin'=>$origin,'store_id'=>$storeId))}/order_id/" + order_id;
			$.getAjax(json, false, redirect);
		}

		function redirect(data) {
            if(data.urlTo !=''){
                var jump = "{BEESCRM::U('base/store/updateScanNum',array('store_id'=>$storeId))}";
                $.ajax({
                    url: jump,
                    type: 'POST',
                    dataType: 'json',
                    data: {ad_line:data.urlTo},
                    success: function () {
                        location.href = data.urlTo;
                    }
                })
            }else {
                location.href = data.url;
            }
		}
	});
</script>
<include file="footer" />
